package com.knoya.chejian.pda.module.yuzupan

import android.app.ActivityManager
import android.app.AlertDialog
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.util.Log
import android.view.View
import com.knoya.chejian.pda.R
import com.knoya.chejian.pda.activity.BaseActivity
import com.knoya.chejian.pda.minterface.OnUserDataInterface
import com.knoya.chejian.pda.module.chache.ChacheM
import com.knoya.chejian.pda.util.JsonUtil
import com.knoya.chejian.pda.util.Myutil
import kotlinx.android.synthetic.main.activity_yuzupan.*
import kotlinx.android.synthetic.main.dialog_update.view.*
import kotlinx.android.synthetic.main.header_knoya.*
import org.json.JSONObject
import java.lang.Exception

import com.qs.wiget.PrintUtils
import org.xutils.x.app
import androidx.core.app.ComponentActivity
import androidx.core.app.ComponentActivity.ExtraData
import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import android.os.Build


class yuzupanActivity : BaseActivity(), View.OnClickListener{
    lateinit var M : yuzupanM
    lateinit var dialog: AlertDialog
    lateinit var cangku : String
    override fun getLayoutId(): Int {
        return R.layout.activity_yuzupan
    }

    override fun initCreate() {

        M = yuzupanM()
        iv_header_left.visibility = View.VISIBLE
        iv_header_left.setOnClickListener(this)
        tv_header_title.text = "预组盘"
//        Btsaoma.setOnClickListener(this)
        cangku = sp.getString("yuzupancangku","")
        mBoradcastReceiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
                if (!isAPPBroughtToBackground()) {
                    return
                }
                if (intent.action == "nlscan.action.SCANNER_RESULT" && intent.hasExtra("SCAN_BARCODE1")) {
                    val scanResult_1 = intent.getStringExtra("SCAN_BARCODE1").trim()
                    saomiaoOk(scanResult_1)
                }
                if (intent.action == "android.intent.ACTION_DECODE_DATA" && intent.hasExtra("barcode_string")) {
                    val scanResult = intent.getStringExtra("barcode_string").trim()
                    saomiaoOk(scanResult)
                }
                if(intent.action == "com.qs.scancode"){
                    val scanResult = intent.extras.getString("data").trim()
                    saomiaoOk(scanResult)
                }
            }
        }
    }

    fun saomiaoOk(d:String){
        var data = Myutil.fengehtttppcode(d)
        dialog = Myutil.showupdatedialog(this@yuzupanActivity, "正在查找仓位", false)
        Tv_Pcode.text = data
        M.yuzupan(this,data,cangku,object :OnUserDataInterface{
            override fun onUserData(data: String?) {
                try {
                    var jo = JSONObject(data)
                    if(JsonUtil.getvolue(jo,"status") == "error"){
                        Tv_Position.text = JsonUtil.getvolue(jo,"msg")
                    }else{
                        var position = JsonUtil.getvolue(jo,"position_buffer")
                        Tv_Position.text = position
                        if(Build.MODEL == "5501L") {
                            PrintUtils.printText(2, 1,  position, true)
//                            PrintUtils.printQRCode(1, 200, 200, position, true,25,true)
//                          PrintUtils.printBarCode(1, 384, 100, position, true,25,true)
                        }
                    }
                }catch (e:Exception){
                    e.printStackTrace()
                }
                dialog.dismiss()
            }

            override fun onNot(throwable: String?) {
                dialog.dismiss()
            }

        })
    }

    override fun onClick(v: View?) {
        when(v){
            iv_header_left -> {
                finish()
            }
//            Btsaoma -> {
//                saomiaoOk(Etshuru.text.toString())
//            }
        }
    }

    fun isAPPBroughtToBackground(): Boolean {
        val am = this.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val tasks = am.getRunningTasks(1)
        if (!tasks.isEmpty()) {
            val topActivity = tasks[0].topActivity
            if (topActivity.className == this.javaClass.name) {

                return true
            }
        }
        return false
    }
}